﻿namespace IESN.Communication
{
    using Microsoft.VisualBasic;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.CompilerServices;

    public class Comm : IPLC
    {
        [AccessedThroughProperty("mRs232")]
        private Rs232 _mRs232;
        private short _PLCMode;
        private short Base_Response_time_interval;
        private short DBNo1;
        private short LinkAdd = 0;
        private short Max_Every_Read_Unit = 0;
        private short Max_Every_Write_Unit = 0;
        private bool mReadFinished;
        private bool mWriteFinished;
        public short Status_LResetbit = 0;
        public short Status_LRread = 0;
        public short Status_LRwrite = 0;
        public short Status_LSetbit = 0;
        private short Status_NRead1 = 0;
        private short Status_NResetbit1 = 0;
        private short Status_NSetbit1 = 0;
        private short Status_NWrite1 = 0;
        public short Status_StatusRead = 0;
        public short Status_StatusWrite = 0;
        private short Status1;

        public Comm(short intLinkAddr, short intPort, int intBaudRate, short intTimeOut)
        {
            this.mRs232 = new Rs232();
            this.mReadFinished = false;
            this.mWriteFinished = false;
            this._PLCMode = 0x3e7;
            this.LinkAdd = intLinkAddr;
            Rs232 rs = this.mRs232;
            rs.Port = intPort;
            rs.BaudRate = intBaudRate;
            rs.Parity = Rs232.DataParity.Parity_Even;
            rs.DataBit = 7;
            rs.StopBit = Rs232.DataStopBit.StopBit_2;
            rs.BufferSize = 0x400;
            rs.Timeout = intTimeOut;
            rs.WorkingMode = Rs232.Mode.Overlapped;
            if (!rs.IsOpen)
            {
                rs.Open();
            }
            rs = null;
        }

        public void AbortC1000()
        {
            string str = this.BuildHeader(this.LinkAdd) + "XZ";
            str = this.SetFrameCheckSequence(str) + "*" + "\r";
            Application.DoEvents();
            try
            {
                this.mRs232.Write(str);
                Thread.Sleep(100);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                ProjectData.ClearProjectError();
            }
            Application.DoEvents();
        }

        public string BToC500(string bcode, short numberofbcodedigit)
        {
            short num2;
            string str3 = "";
            if (numberofbcodedigit <= 0)
            {
                return str3;
            }
            short num = Convert.ToInt16(bcode.Length);
            if (num > 0)
            {
                short num5 = num;
                for (num2 = 1; num2 <= num5; num2 = (short) (num2 + 1))
                {
                    string str4 = Conversion.Str(Strings.Asc(Strings.UCase(Strings.Mid(bcode, num2, 1)))); 
                    str4 = str4.Substring(str4.Length - 2, 2);
                    str3 = str3 + str4;
                }
            }
            if ((numberofbcodedigit % 2) == 0)
            {
                if (numberofbcodedigit > num)
                {
                    short num4 = num;
                    for (num2 = Convert.ToInt16((int) (numberofbcodedigit - 1)); num2 >= num4; num2 = (short) (num2 + -1))
                    {
                        if (StringType.StrCmp(bcode, "", false) == 0)
                        {
                            str3 = str3 + "00";
                        }
                        else
                        {
                            str3 = str3 + "32";
                        }
                    }
                }
                return str3;
            }
            if ((numberofbcodedigit % 2) != 1)
            {
                return str3;
            }
            if (numberofbcodedigit > num)
            {
                short num3 = num;
                for (num2 = numberofbcodedigit; num2 >= num3; num2 = (short) (num2 + -1))
                {
                    if (StringType.StrCmp(bcode, "", false) == 0)
                    {
                        str3 = str3 + "00";
                    }
                    else
                    {
                        str3 = str3 + "32";
                    }
                }
                return str3;
            }
            if (StringType.StrCmp(bcode, "", false) == 0)
            {
                return (str3 + "00");
            }
            return (str3 + "32");
        }

        public string BuildHeader(short linkeraddress)
        {
            StringBuilder builder = new StringBuilder("");
            builder.Append("@");
            builder.Append(Strings.Format(linkeraddress, "0#"));
            return builder.ToString();
        }

        public string C1000Command(short COMMANDTYPE, short startdmaddress, short dmcount, string inout)
        {
            string str=string.Empty;
            short num4=0;
            StringBuilder builder = new StringBuilder("");
            StringBuilder builder2 = new StringBuilder("");
            short num2 = -1;
            try
            {
                short num6=0;
                this.Status = 0;
                if (COMMANDTYPE == 1)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("RD");
                    builder.Append(this.Dec4(startdmaddress));
                    builder.Append(this.Dec4(dmcount));
                    num6 = (short) Math.Round((double) ((Conversion.Val(Strings.Mid(builder.ToString(), 10, 4)) * 4.0) + 11.0));
                }
                else if (COMMANDTYPE == 2)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("WD");
                    builder.Append(this.Dec4(startdmaddress));
                    builder.Append(inout);
                    num6 = 11;
                }
                else if (COMMANDTYPE == 3)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("MS");
                    num6 = 15;
                }
                else if (COMMANDTYPE == 4)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("SC");
                    builder.Append(inout);
                    num6 = 11;
                }
                else if (COMMANDTYPE == 5)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("RL");
                    builder.Append(this.Dec4(startdmaddress));
                    builder.Append(this.Dec4(dmcount));
                    num6 = (short) Math.Round((double) ((Conversion.Val(Strings.Mid(builder.ToString(), 10, 4)) * 4.0) + 11.0));
                }
                else if (COMMANDTYPE == 6)
                {
                    builder.Append(this.BuildHeader(this.LinkAdd));
                    builder.Append("WL");
                    builder.Append(this.Dec4(startdmaddress));
                    builder.Append(inout);
                    num6 = 11;
                }
                builder2.Append(this.SetFrameCheckSequence(builder.ToString()));
                builder2.Append("*");
                builder2.Append('\r');
                string sRight = builder2.ToString().Substring(0, 5) + "00";
                short num7 = 0;
                num4 = 1;
                this.mRs232.ClearInputBuffer();
                do
                {
                    try
                    {
                        this.mRs232.Write(builder2.ToString());
                        do
                        {
                            Application.DoEvents();
                        }
                        while (!this.mWriteFinished);
                        this.mWriteFinished = false;
                    }
                    catch (Exception exception1)
                    {
                        ProjectData.SetProjectError(exception1);
                        ProjectData.ClearProjectError();
                    }
                    finally
                    {
                        this.mWriteFinished = false;
                    }
                    Thread.Sleep(10);
                    try
                    {
                        num2 = (short) this.mRs232.Read(num6);
                        do
                        {
                            Application.DoEvents();
                        }
                        while (!(this.mReadFinished & (num2 != -1)));
                        this.mReadFinished = false;
                    }
                    catch (Exception exception3)
                    {
                        ProjectData.SetProjectError(exception3);
                        ProjectData.ClearProjectError();
                    }
                    finally
                    {
                        this.mReadFinished = false;
                    }
                    Thread.Sleep(10);
                    string inputStreamString = this.mRs232.InputStreamString;
                    this.mRs232.ClearInputBuffer();
                    try
                    {
                        if (StringType.StrCmp(inputStreamString.Substring(0, 7), sRight, false) == 0)
                        {
                            if (this.ChkFrameCheckSequence(inputStreamString) > 0)
                            {
                                num4 = 0;
                                inputStreamString = inputStreamString.Substring(7, inputStreamString.Length - 11);
                                this.Status = 1;
                                this.mWriteFinished = false;
                                this.mReadFinished = false;
                                builder = null;
                                builder2 = null;
                                return inputStreamString;
                            }
                            Debug.WriteLine("Error type -->> 1");
                            num7 = (short) (num7 + 1);
                        }
                        else
                        {
                            num7 = (short) (num7 + 1);
                        }
                    }
                    catch (Exception exception4)
                    {
                        ProjectData.SetProjectError(exception4);
                        Exception exception = exception4;
                        Debug.WriteLine("Error type --->> 3");
                        Console.WriteLine(exception.Message);
                        num7 = (short) (num7 + 1);
                        ProjectData.ClearProjectError();
                    }
                    if (num7 > 6)
                    {
                        Debug.WriteLine("Error type --->> 4");
                        num4 = 0;
                        this.Status = 0;
                        Application.DoEvents();
                        this.AbortC1000();
                        this.mRs232.ClearInputBuffer();
                        Application.DoEvents();
                        this.mWriteFinished = false;
                        this.mReadFinished = false;
                        builder = null;
                        builder2 = null;
                        return "";
                    }
                }
                while (num4 != 0);
            }
            catch (Exception exception5)
            {
                ProjectData.SetProjectError(exception5);
                Exception objException = exception5;
                this.LogError(objException);
                Debug.WriteLine("Error type --->> 5");
                num4 = 0;
                this.Status = 0;
                Application.DoEvents();
                this.AbortC1000();
                this.mRs232.ClearInputBuffer();
                Application.DoEvents();
                this.mWriteFinished = false;
                this.mReadFinished = false;
                builder = null;
                builder2 = null;
                str = "";
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public string C500Pad(short X)
        {
            string expression = Strings.LTrim(Strings.RTrim(Conversion.Str(X)));
            short num = (short) Strings.Len(expression);
            return (new string('0', 4 - num) + expression);
        }

        public string C500ToB(string C5data, short NumOfBcodeDigit)
        {
            string str = "";
            int num = 0;
            if (NumOfBcodeDigit > 0)
            {
                short num3 = Convert.ToInt16((int) (NumOfBcodeDigit * 2));
                for (short i = 1; i <= num3; i = (short) (i + 2))
                {
                    str = str + StringType.FromChar(Strings.Chr(Convert.ToInt32(Conversion.Val(Strings.Mid(C5data, i, 2)))));
                    num += Convert.ToInt32(Conversion.Val(Strings.Mid(C5data, i, 2)));
                }
            }
            if (num == 0)
            {
                str = "";
            }
            return str;
        }

        public short Check_PLC_Mode()
        {
            short num=0;
            short num2 = 0;
            short num3 = 0;
            string str = this.StatusRead();
            num2 = this.Status_StatusRead;
            if (num2 == 1)
            {
                if (StringType.StrCmp(Strings.Mid(str, 2, 1), "3", false) == 0)
                {
                    num = 3;
                    Application.DoEvents();
                    this.StatusWrite(3);
                    Application.DoEvents();
                    num3 = this.Status_StatusWrite;
                }
                if (StringType.StrCmp(Strings.Mid(str, 2, 1), "2", false) == 0)
                {
                    num = 2;
                    Application.DoEvents();
                    this.StatusWrite(4);
                    Application.DoEvents();
                    num3 = this.Status_StatusWrite;
                }
                if (StringType.StrCmp(Strings.Mid(str, 2, 1), "0", false) == 0)
                {
                    num = 0;
                    Application.DoEvents();
                    this.StatusWrite(1);
                    Application.DoEvents();
                    num3 = this.Status_StatusWrite;
                }
            }
            if ((num2 == 1) & (num3 == 1))
            {
                if (num == 2)
                {
                    this.StatusWrite(3);
                    Application.DoEvents();
                    num3 = this.Status_StatusWrite;
                }
            }
            else if ((num2 == 1) & (num3 == 0))
            {
                num = -1;
            }
            else if ((num2 == 0) & (num3 == 0))
            {
                num = -2;
            }
            Application.DoEvents();
            return num;
        }

        public short ChkFrameCheckSequence(string c_response)
        {
            short number = 0;
            short num3 = (short) (Strings.Len(c_response) - 4);
            string sLeft = Strings.Mid(c_response, num3 + 1, 2);
            short num5 = num3;
            for (short i = 1; i <= num5; i = (short) (i + 1))
            {
                number = (short) (number ^ Strings.Asc(Strings.Mid(c_response, i, 1)));
            }
            string str = Conversion.Hex(number);
            if (StringType.StrCmp(sLeft, str.Substring(str.Length - 2, 2), false) == 0)
            {
                return -1;
            }
            return 0;
        }

        public void CloseComm()
        {
            if (this.mRs232.IsOpen)
            {
                this.mRs232.Close();
            }
        }

        private void CompleteRead(Rs232 Source, byte[] DataBuffer)
        {
            this.mReadFinished = true;
        }

        private void CompleteWrite(Rs232 Source)
        {
            this.mWriteFinished = true;
        }

        public string Dec4(short INinteger)
        {
            if ((INinteger >= 0) & (INinteger <= 0x270f))
            {
                return Strings.Format(INinteger, "0###");
            }
            return "9999";
        }

        public void Init_Comm_Para(short p_Base_Response_time_interval, short p_Max_Every_Read_Unit, short p_Max_Every_Write_Unit)
        {
            this.Base_Response_time_interval = p_Base_Response_time_interval;
            this.Max_Every_Read_Unit = p_Max_Every_Read_Unit;
            this.Max_Every_Write_Unit = p_Max_Every_Write_Unit;
        }

        private void LogError(Exception objException)
        {
            try
            {
                Exception exception = objException;
                string prompt = (("An error occurred in the following module:  --  \r\n Source:  " + exception.Source + "\r\n Message:  " + exception.Message) + "\r\nStack Trace :   " + exception.StackTrace) + "\r\nTarget Site :   " + exception.TargetSite.ToString();
                exception = null;
                Interaction.MsgBox(prompt, MsgBoxStyle.ApplicationModal, null);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                ProjectData.ClearProjectError();
            }
        }

        public string Lresetbit(short LRaddress, short bitnumber)
        {
            short[] numArray = new short[5];
            this.Status_LResetbit = 0;
            string str = this.C1000Command(5, LRaddress, 1, "");
            if (this.Status == 1)
            {
                numArray[1] = Convert.ToInt16("&H" + Strings.Mid(str, 1, 1));
                numArray[2] = Convert.ToInt16("&H" + Strings.Mid(str, 2, 1));
                numArray[3] = Convert.ToInt16("&H" + Strings.Mid(str, 3, 1));
                numArray[4] = Convert.ToInt16("&H" + Strings.Mid(str, 4, 1));
                if ((bitnumber >= 0) & (bitnumber <= 3))
                {
                    numArray[4] = (short) (numArray[4] & Convert.ToInt16((double) (15.0 - Math.Pow(2.0, (double) bitnumber))));
                }
                if ((bitnumber >= 4) & (bitnumber <= 7))
                {
                    numArray[3] = (short) (numArray[3] & Convert.ToInt16((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 4)))));
                }
                if ((bitnumber >= 8) & (bitnumber <= 11))
                {
                    numArray[2] = (short) (numArray[2] & Convert.ToInt16((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 8)))));
                }
                if ((bitnumber >= 12) & (bitnumber <= 15))
                {
                    numArray[1] = (short) (numArray[1] & Convert.ToInt16((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 12)))));
                }
                str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                string str3 = this.C1000Command(6, LRaddress, 1, str);
                this.Status_LResetbit = this.Status;
            }
            else
            {
                this.Status_LResetbit = 0;
            }
            return "";
        }

        public string LRread(short startLRaddress, short LRcount)
        {
            string str=string.Empty;
            this.Status_LRread = 0;
            short startdmaddress = startLRaddress;
            if ((LRcount >= 1) & (LRcount <= 0x40))
            {
                startdmaddress = startLRaddress;
                short falsePart = LRcount;
                str = "";
                do
                {
                    string str2 = this.C1000Command(5, startdmaddress, ShortType.FromObject(Interaction.IIf(falsePart >= 0x10, 0x10, falsePart)), "");
                    this.Status_LRread = this.Status;
                    str = str + str2;
                    startdmaddress = (short) (startdmaddress + Convert.ToInt16(0x10));
                    falsePart = (short) (falsePart - 0x10);
                }
                while (!((falsePart <= 0) | (this.Status_LRread == 0)));
                return str;
            }
            str = "";
            this.Status_LRread = 0;
            return str;
        }

        public string LRwrite(short start_LR_address, short LR_count, string in_data)
        {
            this.Status_LRwrite = 0;
            short truePart = (short) Math.Round((double) (((double) Strings.Len(in_data)) / 4.0));
            if (LR_count > 0)
            {
                truePart = ShortType.FromObject(Interaction.IIf(LR_count >= truePart, truePart, LR_count));
            }
            if ((truePart >= 1) & (truePart <= 0x40))
            {
                short startdmaddress = start_LR_address;
                short start = 1;
                do
                {
                    string inout = Strings.Mid(in_data, start, IntegerType.FromObject(ObjectType.MulObj(Interaction.IIf(truePart >= 0x10, 0x10, truePart), 4)));
                    short dmcount = ShortType.FromObject(Interaction.IIf(truePart >= 0x10, 0x10, truePart));
                    string str3 = this.C1000Command(6, startdmaddress, dmcount, inout);
                    this.Status_LRwrite = this.Status;
                    startdmaddress = (short) (startdmaddress + 0x10);
                    start = (short) (start + 0x40);
                    truePart = (short) (truePart - 0x10);
                }
                while (!((truePart <= 0) | (this.Status_LRwrite == 0)));
            }
            else
            {
                this.Status_LRwrite = 0;
            }
            return "";
        }

        public string Lsetbit(short LRaddress, short bitnumber)
        {
            short[] numArray = new short[5];
            this.Status_LSetbit = 0;
            string str = this.C1000Command(5, LRaddress, 1, "");
            if (this.Status == 1)
            {
                numArray[1] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 1, 1)));
                numArray[2] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 2, 1)));
                numArray[3] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 3, 1)));
                numArray[4] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 4, 1)));
                if ((bitnumber >= 0) & (bitnumber <= 3))
                {
                    numArray[4] = (short) (numArray[4] | ((long) Math.Round(Math.Pow(2.0, (double) bitnumber))));
                }
                if ((bitnumber >= 4) & (bitnumber <= 7))
                {
                    numArray[3] = (short) (numArray[3] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 4)))));
                }
                if ((bitnumber >= 8) & (bitnumber <= 11))
                {
                    numArray[2] = (short) (numArray[2] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 8)))));
                }
                if ((bitnumber >= 12) & (bitnumber <= 15))
                {
                    numArray[1] = (short) (numArray[1] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 12)))));
                }
                str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                string str3 = this.C1000Command(6, LRaddress, 1, str);
                if (this.Status == 1)
                {
                    this.Status_LSetbit = 1;
                }
                else
                {
                    this.Status_LSetbit = 0;
                }
            }
            else
            {
                this.Status_LSetbit = 0;
            }
            return "";
        }

        public string NReadWord(short start_dm_address, short dm_count)
        {
            string str=string.Empty;
            StringBuilder builder = new StringBuilder("");
            try
            {
                if (this.Max_Every_Read_Unit < 10)
                {
                    this.Max_Every_Read_Unit = 0x1d;
                }
                this.Status_NRead = 0;
                short startdmaddress = start_dm_address;
                short dmcount = dm_count;
                if (dmcount <= 0)
                {
                    goto Label_011B;
                }
                if (dmcount > 100)
                {
                    goto Label_010C;
                }
                if (dmcount > this.Max_Every_Read_Unit)
                {
                    goto Label_00BA;
                }
                string str2 = this.C1000Command(1, startdmaddress, dmcount, "a");
                if (this.Status == 1)
                {
                    this.Status_NRead = 1;
                    return str2;
                }
                this.Status_NRead = 0;
                return "";
            Label_007F:
                builder.Append(this.C1000Command(1, startdmaddress, this.Max_Every_Read_Unit, "a"));
                startdmaddress = (short) (startdmaddress + this.Max_Every_Read_Unit);
                dmcount = (short) (dmcount - this.Max_Every_Read_Unit);
                if (this.Status == 0)
                {
                    goto Label_00C3;
                }
            Label_00BA:
                if (dmcount > this.Max_Every_Read_Unit)
                {
                    goto Label_007F;
                }
            Label_00C3:
                if (this.Status == 1)
                {
                    builder.Append(this.C1000Command(1, startdmaddress, dmcount, "a"));
                }
                if (this.Status == 1)
                {
                    this.Status_NRead = 1;
                    return builder.ToString();
                }
                this.Status_NRead = 0;
                return "";
            Label_010C:
                this.Status_NRead = 0;
                return "";
            Label_011B:
                this.Status_NRead = 0;
                str = "";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.LogError(objException);
                this.Status_NRead = 0;
                str = "";
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public bool NReadWord(short start_dm_address, short dm_count, ref byte[] value_byte)
        {
            bool flag=false;
            return flag;
        }

        public string NReadWord(short start_dm_address, short dm_count, string DataType)
        {
            string str=string.Empty;
            return str;
        }

        public string NResetBit(short dmaddress, short bitnumber)
        {
            string str2=string.Empty;
            string str = "";
            short[] numArray = new short[5];
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 1);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 3);
                bitnumber = (short) (bitnumber - 0x30);
            }
            try
            {
                this.Status_NResetbit = 0;
                str = this.C1000Command(1, dmaddress, 1, "");
                if (this.Status == 1)
                {
                    numArray[1] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 1, 1)));
                    numArray[2] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 2, 1)));
                    numArray[3] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 3, 1)));
                    numArray[4] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 4, 1)));
                    if ((bitnumber >= 0) & (bitnumber <= 3))
                    {
                        numArray[4] = (short) (numArray[4] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) bitnumber)))));
                    }
                    if ((bitnumber >= 4) & (bitnumber <= 7))
                    {
                        numArray[3] = (short) (numArray[3] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 4))))));
                    }
                    if ((bitnumber >= 8) & (bitnumber <= 11))
                    {
                        numArray[2] = (short) (numArray[2] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 8))))));
                    }
                    if ((bitnumber >= 12) & (bitnumber <= 15))
                    {
                        numArray[1] = (short) (numArray[1] & ((long) Math.Round((double) (15.0 - Math.Pow(2.0, (double) (bitnumber - 12))))));
                    }
                    str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                    string str3 = this.C1000Command(2, dmaddress, 1, str);
                    this.Status_NResetbit = this.Status;
                }
                else
                {
                    this.Status_NResetbit = 0;
                }
                str2 = "";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.LogError(objException);
                this.Status_NResetbit = 0;
                str2 = "";
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        public string NSetBit(short dmaddress, short bitnumber)
        {
            string str2=string.Empty;
            short[] numArray = new short[5];
            if ((bitnumber >= 0x10) & (bitnumber <= 0x1f))
            {
                dmaddress = (short) (dmaddress + 1);
                bitnumber = (short) (bitnumber - 0x10);
            }
            if ((bitnumber >= 0x20) & (bitnumber <= 0x2f))
            {
                dmaddress = (short) (dmaddress + 2);
                bitnumber = (short) (bitnumber - 0x20);
            }
            if ((bitnumber >= 0x30) & (bitnumber <= 0x3f))
            {
                dmaddress = (short) (dmaddress + 3);
                bitnumber = (short) (bitnumber - 0x30);
            }
            try
            {
                this.Status_NSetbit = 0;
                string str = this.C1000Command(1, dmaddress, 1, "");
                if (this.Status == 1)
                {
                    numArray[1] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 1, 1)));
                    numArray[2] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 2, 1)));
                    numArray[3] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 3, 1)));
                    numArray[4] = (short) Math.Round(Conversion.Val("&H" + Strings.Mid(str, 4, 1)));
                    if ((bitnumber >= 0) & (bitnumber <= 3))
                    {
                        numArray[4] = (short) (numArray[4] | ((long) Math.Round(Math.Pow(2.0, (double) bitnumber))));
                    }
                    if ((bitnumber >= 4) & (bitnumber <= 7))
                    {
                        numArray[3] = (short) (numArray[3] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 4)))));
                    }
                    if ((bitnumber >= 8) & (bitnumber <= 11))
                    {
                        numArray[2] = (short) (numArray[2] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 8)))));
                    }
                    if ((bitnumber >= 12) & (bitnumber <= 15))
                    {
                        numArray[1] = (short) (numArray[1] | ((long) Math.Round(Math.Pow(2.0, (double) (bitnumber - 12)))));
                    }
                    str = Conversion.Hex(numArray[1]) + Conversion.Hex(numArray[2]) + Conversion.Hex(numArray[3]) + Conversion.Hex(numArray[4]);
                    string str3 = this.C1000Command(2, dmaddress, 1, str);
                    if (this.Status == 1)
                    {
                        this.Status_NSetbit = 1;
                    }
                    else
                    {
                        this.Status_NSetbit = 0;
                    }
                }
                else
                {
                    this.Status_NSetbit = 0;
                }
                str2 = "";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.LogError(objException);
                this.Status_NSetbit = 0;
                str2 = "";
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        public bool NWriteWord(short start_dm_address, short dm_count, byte[] SendArry)
        {
            bool flag=false;
            return flag;
        }

        public string NWriteWord(short start_dm_address, short dm_count, string Indata)
        {
            string str3=string.Empty;
            try
            {
                short num=0;
                if (this.Max_Every_Write_Unit < 10)
                {
                    this.Max_Every_Write_Unit = 0x1d;
                }
                this.Status_NWrite = 0;
                string inout = Indata;
                short startdmaddress = start_dm_address;
                if (dm_count == 0)
                {
                    if ((inout.Length % 4) == 0)
                    {
                        num = (short) Math.Round(Conversion.Int((double) (((double) inout.Length) / 4.0)));
                    }
                    else
                    {
                        num = (short) Math.Round((double) (Conversion.Int((double) (((double) inout.Length) / 4.0)) + 1.0));
                    }
                }
                else
                {
                    num = dm_count;
                }
                if (num > 0)
                {
                    if (num <= 100)
                    {
                        string str2;
                        if (num <= this.Max_Every_Write_Unit)
                        {
                            str2 = this.C1000Command(2, startdmaddress, num, inout);
                            if (this.Status == 1)
                            {
                                this.Status_NWrite = 1;
                            }
                            else
                            {
                                this.Status_NWrite = 0;
                            }
                        }
                        else
                        {
                            short start = 1;
                            while (num > this.Max_Every_Write_Unit)
                            {
                                str2 = this.C1000Command(2, startdmaddress, this.Max_Every_Write_Unit, Strings.Mid(inout, start, this.Max_Every_Write_Unit * 4));
                                startdmaddress = (short) (startdmaddress + this.Max_Every_Write_Unit);
                                num = (short) (num - this.Max_Every_Write_Unit);
                                start = (short) (start + (this.Max_Every_Write_Unit * 4));
                                if (this.Status == 0)
                                {
                                    break;
                                }
                            }
                            if (this.Status == 1)
                            {
                                str2 = this.C1000Command(2, startdmaddress, num, Strings.Mid(inout, start, num * 4));
                            }
                            if (this.Status == 1)
                            {
                                this.Status_NWrite = 1;
                            }
                            else
                            {
                                this.Status_NWrite = 0;
                            }
                        }
                    }
                    else
                    {
                        this.Status_NWrite = 0;
                    }
                }
                else
                {
                    this.Status_NWrite = 0;
                }
                str3 = "";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.LogError(objException);
                this.Status_NWrite = 0;
                str3 = "";
                ProjectData.ClearProjectError();
            }
            return str3;
        }

        public string NWriteWord(short start_dm_address, short dm_count, string Indata, string DataType)
        {
            string str=string.Empty;
            return str;
        }

        public string SetFrameCheckSequence(string cs_xmit)
        {
            short number = 0;
            short num3 = (short) Strings.Len(cs_xmit);
            for (short i = 1; i <= num3; i = (short) (i + 1))
            {
                number = (short) (number ^ Strings.Asc(Strings.Mid(cs_xmit, i, 1)));
            }
            string str = Conversion.Hex(number);
            return (cs_xmit + str.Substring(str.Length - 2, 2));
        }

        public string StatusRead()
        {
            string str2;
            this.Status_StatusRead = 0;
            string str = this.C1000Command(3, 0, 0, "");
            try
            {
                str2 = str.Substring(0, 4);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                str2 = "";
                ProjectData.ClearProjectError();
            }
            this.Status_StatusRead = this.Status;
            return str2;
        }

        public short StatusWrite(short COMMANDTYPE)
        {
            string str=string.Empty;
            short num=0;
            this.Status_StatusWrite = 0;
            if (COMMANDTYPE == 1)
            {
                str = "00";
            }
            else if (COMMANDTYPE == 2)
            {
                str = "01";
            }
            else if (COMMANDTYPE == 3)
            {
                str = "02";
            }
            else if (COMMANDTYPE == 4)
            {
                str = "03";
            }
            string str2 = this.C1000Command(4, 0, 0, str);
            this.Status_StatusWrite = this.Status;
            return num;
        }

        public string SwapWord(string DMstr)
        {
            string str = "";
            short num3 = (short) Math.Round((double) (((double) Strings.Len(DMstr)) / 4.0));
            for (short i = 1; i <= num3; i = (short) (i + 1))
            {
                short start = (short) (((i - 1) * 4) + 1);
                str = str + Strings.Mid(DMstr, start + 2, 2) + Strings.Mid(DMstr, start, 2);
            }
            return str;
        }

        public object writeBitArea(short startOffset, short startBitOffset, short bitSize, string WriteData)
        {
            object obj2=null;
            return obj2;
        }

        public short DBNo
        {
            get
            {
                return this.DBNo1;
            }
            set
            {
                this.DBNo1 = value;
            }
        }

        private Rs232 mRs232
        {
            get
            {
                return this._mRs232;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._mRs232 != null)
                {
                    this._mRs232.TxCompletedEvent -= new Rs232.TxCompletedEventHandler(this.CompleteWrite);
                    this._mRs232.DataReceivedEvent -= new Rs232.DataReceivedEventHandler(this.CompleteRead);
                }
                this._mRs232 = value;
                if (this._mRs232 != null)
                {
                    this._mRs232.TxCompletedEvent += new Rs232.TxCompletedEventHandler(this.CompleteWrite);
                    this._mRs232.DataReceivedEvent += new Rs232.DataReceivedEventHandler(this.CompleteRead);
                }
            }
        }

        public short PLCMode
        {
            get
            {
                this._PLCMode = this.Check_PLC_Mode();
                if ((this._PLCMode == -1) | (this._PLCMode == -2))
                {
                    this._PLCMode = 0x3e7;
                }
                return this._PLCMode;
            }
        }

        public short Status
        {
            get
            {
                return this.Status1;
            }
            set
            {
                this.Status1 = value;
            }
        }

        public short Status_NRead
        {
            get
            {
                return this.Status_NRead1;
            }
            set
            {
                this.Status_NRead1 = value;
            }
        }

        public short Status_NResetbit
        {
            get
            {
                return this.Status_NResetbit1;
            }
            set
            {
                this.Status_NResetbit1 = value;
            }
        }

        public short Status_NSetbit
        {
            get
            {
                return this.Status_NSetbit1;
            }
            set
            {
                this.Status_NSetbit1 = value;
            }
        }

        public short Status_NWrite
        {
            get
            {
                return this.Status_NWrite1;
            }
            set
            {
                this.Status_NWrite1 = value;
            }
        }

        //public short VBNET.HowTo.IPLC.DBNo
        //{
        //    get
        //    {
        //        return this.DBNo1;
        //    }
        //    set
        //    {
        //        this.DBNo1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status
        //{
        //    get
        //    {
        //        return this.Status1;
        //    }
        //    set
        //    {
        //        this.Status1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NRead
        //{
        //    get
        //    {
        //        return this.Status_NRead1;
        //    }
        //    set
        //    {
        //        this.Status_NRead1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NResetbit
        //{
        //    get
        //    {
        //        return this.Status_NResetbit1;
        //    }
        //    set
        //    {
        //        this.Status_NResetbit1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NSetbit
        //{
        //    get
        //    {
        //        return this.Status_NSetbit1;
        //    }
        //    set
        //    {
        //        this.Status_NSetbit1 = value;
        //    }
        //}

        //public short VBNET.HowTo.IPLC.Status_NWrite
        //{
        //    get
        //    {
        //        return this.Status_NWrite1;
        //    }
        //    set
        //    {
        //        this.Status_NWrite1 = value;
        //    }
        //}
    }
}

